package mouseBehaviors;

import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import com.sun.j3d.utils.picking.behaviors.PickingCallback;

public class MyPickZoomscaleBehavior extends PickMouseBehavior implements
		MouseBehaviorCallback {

	MyMouseZoomscale wheel;
	//  int pickMode = PickTool.BOUNDS;
	private PickingCallback callback=null;
	private TransformGroup currentTG;
	
	
	public MyPickZoomscaleBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds) {
		super(canvas, root, bounds);
		wheel = new MyMouseZoomscale(MyMouseZoomscale.MANUAL_WAKEUP);
		wheel.setTransformGroup(currGrp);
	    currGrp.addChild(wheel);
	    wheel.setSchedulingBounds(bounds);
	    this.setSchedulingBounds(bounds);
	}
	
	 public MyPickZoomscaleBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds, 
			    int pickMode){
		 super(canvas, root, bounds);
		 wheel = new MyMouseZoomscale(MyMouseZoomscale.MANUAL_WAKEUP);
		 wheel.setTransformGroup(currGrp);
		 currGrp.addChild(wheel);
		 wheel.setSchedulingBounds(bounds);
		 this.setSchedulingBounds(bounds);
		 this.setMode(pickMode);
	 }

	@Override
	public void updateScene(int xpos, int ypos) {
		TransformGroup tg = null;
        
	    if (!mevent.isControlDown() && !mevent.isMetaDown()&&!mevent.isAltDown()){
	      //System.out.println("********************");
	      pickCanvas.setShapeLocation(xpos, ypos);
	      PickResult pr = pickCanvas.pickClosest();
	      if ((pr != null) &&
	          ((tg = (TransformGroup)pr.getNode(PickResult.TRANSFORM_GROUP)) 
									!= null) &&
		  (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_READ)) && 
		  (tg.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))){
		
	    	  wheel.setTransformGroup(tg);
	    	  wheel.wakeup();
	    	  currentTG = tg;
	    	  // Need to clean up Issue 123 --- Chien        
	    	  // freePickResult(pr);
	      } else if (callback!=null)
	    	  callback.transformChanged( PickingCallback.NO_PICK, null );
	    }
	}

	@Override
	public void transformChanged(int type, Transform3D transform) {
		//callback.transformChanged( PickingCallback.TRANSLATE, currentTG );
	}

	public void setupCallback( PickingCallback callback ) {
	      this.callback = callback;
	      if (callback==null)
	    	  wheel.setupCallback( null );
	      else
	    	  wheel.setupCallback( this );
}
}
